
package com.calefk.util {
	
    import com.calefk.core.Library;
	import flash.media.SoundChannel;
    import flash.media.SoundMixer;
	import flash.utils.getDefinitionByName;
	import flash.media.Sound;
	import flash.events.Event;
	import flash.media.SoundTransform;
	
	public class SoundManager {
		
		// public static var channels:Array = new Array();
		public static var soundNames:Array = new Array();
        
        private static var isMute:Boolean = false;
        
        private static var channels:Array  = new Array();;
		
		public static function playSound(soundEffect:String, loop:int = 1 , volumen:Number = 1 , lib_sound:Library = null):void {
            var soundClass: Class = null;
            if (lib_sound == null) {
                soundClass = getDefinitionByName(soundEffect) as Class;
            } else {
                soundClass = lib_sound.getClass(soundEffect);
            }
			var effectSound: Sound = new soundClass();
            
            var trans:SoundTransform = new SoundTransform(((isMute)?0:volumen), 0); 
			var channel:SoundChannel = effectSound.play(0, loop , trans);
            
			channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
            var obj:Object = { name:soundEffect , channel:channel , volumen:volumen};
            channels.push(obj);
            /*
            channels.push(channel);
			soundNames.push(soundEffect);
            */
		}
		
		private static function onSoundComplete(e:Event):void {
            var channel:SoundChannel = SoundChannel(e.currentTarget);
            /*
			e.target.stop();
			var ind: int = channels.indexOf(e.target);
			if (ind<0) {return;}
			channels.splice(ind,1);
			soundNames.splice(ind,1);
            */
            
            // se borra el canal
            var size:int = channels.length;
            for (var k:int = 0; k < size; k++) {
                var obj:* = channels[k];
                if (obj.channel == channel) {
                    channels.splice(k , 1);
                    break;
                }
            }
		}
		
		public static function stopSound(soundEffect:String):void {
            var size:int = channels.length;
            for (var k:int = 0; k < size; k++) {
                var obj:* = channels[k];
                if (obj.name == soundEffect) {
                    var soundChannel:SoundChannel = SoundChannel(obj.channel);
                    soundChannel.stop();
                    channels.splice(k , 1);
                    break;
                }
            }            
            /*
			try {
				var ind: int = soundNames.indexOf(soundEffect);
				if (ind<0) {return;}
				soundNames.splice(ind,1);
				channels[ind].stop();
				channels.splice(ind, 1);
			} catch (e:*) {
				Logger.error("error SoundManager " + e);
			}*/
		}
		
		/* Funcion que detiene todos los sonidos */
		public static function stopAllSounds():void {
            var size:int = channels.length;
            for (var k:int = 0; k < size; k++) {
                var obj:* = channels[k];
                if (obj.channel) {
                    var soundChannel:SoundChannel = SoundChannel(obj.channel);
                    soundChannel.stop();
                }
            }   
            
            if (channels.length > 0) {
                channels.splice(0 , channels.length);
            }
            /*
			try {
				var leng: int = channels.length;
				for (var i:int = 0; i< leng; i++) {
					channels[i].stop();
					soundNames.splice(i,1);
					channels.splice(i,1);
					i--;
				}
			} catch (e:*) {
				
			}*/
		}
        
        public static function muteAll():void {
            isMute = true;
            var size:int = channels.length;
            for (var k:int = 0; k < size; k++) {
                var obj:* = channels[k];
                if (obj.channel) {
                    var soundChannel:SoundChannel = SoundChannel(obj.channel);
                    soundChannel.soundTransform = new SoundTransform(0);
                }
            }               
        }
        
        public static function unmuteAll():void {
            isMute = false;
            var size:int = channels.length;
            for (var k:int = 0; k < size; k++) {
                var obj:* = channels[k];
                if (obj.channel) {
                    var soundChannel:SoundChannel = SoundChannel(obj.channel);
                    soundChannel.soundTransform = new SoundTransform(obj.volumen);
                }
            }              
        }
        
        public static function forceStopAll():void {
            SoundMixer.stopAll();
        }
	}
}
